<template>
  <div class="table-filter card">
    <el-form ref="filterFormRef" inline :model="filterForm">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item prop="belong" label="字典">
            <DictSelect v-model:selected="filterForm.belong" :list="dictList" label-field="label" value-field="name"></DictSelect>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="encode" label="字典项编码">
            <el-input v-model.trim="filterForm.encode" placeholder="请输入字典项编码" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="label" label="字典项键">
            <el-input v-model.trim="filterForm.label" placeholder="请输入字典项键" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="value" label="字典项值">
            <el-input v-model.trim="filterForm.value" placeholder="请输入字典项值" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" @click="submit">搜索</el-button>
            <el-button type="danger" @click="reset">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import DictSelect from '@/components/DictSelect/DictSelect.vue'
import { dictFindAll } from '@/api/system/dict'

const route = useRoute()
const dictBelong = computed(() => route.query.dict)

const emits = defineEmits(['submit', 'reset'])

const filterFormRef = ref()
// 过滤条件表单
const filterForm = ref({
  belong: dictBelong.value,
  encode: '',
  label: '',
  value: ''
})

onMounted(() => {
  queryDict()
})

const dictList = ref([])
async function queryDict() {
  const res = await dictFindAll({ pageSize: -1, status: 1 })
  dictList.value = res.data
}

// 提交
function submit() {
  emits('submit', filterForm.value)
}

// 重置
function reset() {
  filterFormRef.value.resetFields()
  emits('reset')
}
</script>

<style lang="scss"></style>
